package org.hxyjs.dao.impl;

import org.hxyjs.dao.CarouselmapDao;
import org.hxyjs.entity.Carouselmap;
import org.hxyjs.mapper.CarouselmapMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("carouselmapDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class CarouselmapDaoImpl implements CarouselmapDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private CarouselmapMapper carouselmapMapper;
    public List<Carouselmap>  getAllCarouselmaps() throws Exception{
        return carouselmapMapper.selectByExample(null);
    }

    @Override
    public Integer deleteCarouselmapByCmno(String cmno)  throws Exception{
        return carouselmapMapper.deleteByPrimaryKey(cmno);
    }

    @Override
    public Integer updateCarouselmapByCmno(Carouselmap carouselmap)  throws Exception{
        return carouselmapMapper.updateByPrimaryKey(carouselmap);
    }

    @Override
    public Integer addCarouselmap(Carouselmap carouselmap)  throws Exception{
        carouselmap.setCmno(initId());
        return carouselmapMapper.insertSelective(carouselmap);
    }
    public String initId(){
        String maxCmno = carouselmapMapper.getMaxCmno();
        return GenNumTools.initId("",3,maxCmno);
    }
    @Override
    public List<Carouselmap> getCarouselmapByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)carouselmapMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return carouselmapMapper.getCarouselmapByPage(map);
    }
}
